<?php

namespace App\Admin\Controllers;

use App\Models\CaseModel;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CaseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '优秀案例';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CaseModel());

        $grid->column('id', '编号')->sortable()->width(50);
        $grid->column('title', '标题')->text()->width(200);
        $grid->column('image', '案例图片')->image(env('APP_URL'), 50, 50)->width(100);
        $grid->column('content', '内容')->display(function ($content) {
            return mb_substr(str_replace("&nbsp;", "", strip_tags($content)), 0, 100).'...';
        });
        $grid->column('publish_at', '发布时间')->width(200)->sortable();
        $grid->column('created_at', '创建时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });
        $grid->column('updated_at', '修改时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });

        $grid->disableRowSelector();
        $grid->disableFilter();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CaseModel::findOrFail($id));

        $show->field('id', '编号');
        $show->field('title', '标题');
        $show->field('image', '案例图片')->image();
        $show->field('content', '内容')->unescape();
        $show->field('publish_at', '发布时间');
        $show->field('created_at', '创建时间');
        $show->field('updated_at', '修改时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CaseModel);

        $form->text('title', '标题')->required();

        // 案例图片上传
        $path = 'images/admin/cases';
        $form->image('image', '案例图片')->required()->move($path)->uniqueName()
            ->help('请上传图片，建议尺寸800px×500px，文件大小不超过2MB');

        $form->datetime('publish_at', '发布时间')->default(date('Y-m-d H:i:s'))->required();
        $form->editor('content', '内容')->required();

        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        $form->tools(function ($tools){
            // 去掉`查看`按钮
            $tools->disableView();
        });

        return $form;
    }
}
